package com.hbnu.controller;

import com.hbnu.mapper.PetMapper;
import com.hbnu.mapper.UserMapper;
import com.hbnu.pojo.Pet;
import com.hbnu.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/pet")
public class PetController {
    @Autowired
    private PetService petService;


    //查询当前用户宠物列表(普通用户功能)
    @GetMapping("/list")
    public Map<String, Object> getPetList(@RequestParam Integer ownerId){
        List<Pet> pets =  petService.getPetsByOwnerId(ownerId);
        Map<String ,Object> res  = new HashMap<>();
        res.put("success",  true);
        res.put("data", pets);
        return res;
    }

    //查询所有用户的宠物信息(管理员功能)
     @GetMapping("/all")
     public Map<String, Object> getAllPetList(){
         List<Pet> pets =  petService.getAllPets();
         Map<String ,Object> res  = new HashMap<>();
         res.put("success",  true);
         res.put("data", pets);
         return res;
     }

    //添加宠物信息
    @PostMapping("/add")
    public Map<String, Object> addPet(@RequestBody Pet pet){
        boolean success = petService.addPet(pet);
        Map<String, Object> res  = new HashMap<>();
        res.put("success",  success);
        res.put("message", success ? "添加成功" : "添加失败");
        return res;
    }
    //删除宠物信息
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deletePet(@PathVariable Integer id){
        boolean success = petService.deletePet(id);
        Map<String, Object> res  = new HashMap<>();
        res.put("success",  success);
        res.put("message", success ? "删除成功" : "删除失败");
        return res;
    }


    //修改宠物信息
    @PostMapping("/update")
    public Map<String, Object> updatePet(@RequestBody Pet pet){
        boolean success = petService.updatePet(pet);
        Map<String, Object> res  = new HashMap<>();
        res.put("success",  success);
        res.put("message", success ? "修改成功" : "修改失败");
        return res;
    }
}
